using System;

namespace EastBancTech.InstallerDefinition.Native
{
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    using Microsoft.VisualStudio.Modeling;
    using System.IO;
    using System.Collections.Generic;
    using System.Globalization;

    /// <summary>
    /// Interop for Win32 file system calls.
    /// </summary>
    public static class FileSystemInterop
    {
        internal const int MaxPath = 255;
        private static readonly char[] invalidShortPathChars;

        public const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
        public const int FILE_ATTRIBUTE_NORMAL = 0x80;



        /// <summary>
        /// Cannot instantiate this class.
        /// </summary>
        static FileSystemInterop()
        {
            FileSystemInterop.invalidShortPathChars = new char[] { '+', ',', ';', '=', '[', ']', ' ' };
        }

        /// <summary>
        /// Gets the short name for a file.
        /// </summary>
        /// <param name="longPath">Long path to convert to short path.</param>
        /// <param name="shortPath">Short path from long path.</param>
        /// <param name="buffer">Size of short path.</param>
        /// <returns>zero if success.</returns>
        [DllImport("kernel32.dll", EntryPoint = "GetShortPathNameW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
        internal static extern uint GetShortPathName(string longPath, StringBuilder shortPath, [MarshalAs(UnmanagedType.U4)]int buffer);

        /// <summary>
        /// Gets the long name for a file.
        /// </summary>
        /// <param name="shortPath">Short path to convert to short path.</param>
        /// <param name="longPath">Long path from long path.</param>
        /// <param name="buffer">Size of short path.</param>
        /// <returns>zero if success.</returns>
        [DllImport("kernel32.dll", EntryPoint = "GetLongPathNameW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
        internal static extern uint GetLongPathName(string shortPath, StringBuilder longPath, [MarshalAs(UnmanagedType.U4)]int buffer);

        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int ExtractIconEx(string path, int index, IntPtr smallIcons, IntPtr largeIcons, uint numberOfIcons);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("shlwapi.dll", EntryPoint = "PathRelativePathToW", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool PathRelativePathTo([Out] StringBuilder pszPath, [In] string pszFrom, [In] uint dwAttrFrom, [In] string pszTo, [In] uint dwAttrTo);

        //internal static String GetRelativePath(String fullPath, String rootPath)
        //{
        //    if (rootPath == null)
        //        return fullPath;

        //    int rootIndex = fullPath.IndexOf(rootPath);

        //    if (rootIndex == -1)
        //        return fullPath;

        //    String relativePath = fullPath.Remove(rootIndex,rootPath.Length);
        //    return relativePath.TrimStart(new char[]{'\\','/'});
        //}

        internal static string GetRelativePath(string targetPath, string relativeToPath)
        {
            if (targetPath.Length > 260)
            {
                throw new ArgumentOutOfRangeException("targetPath", string.Format(CultureInfo.CurrentCulture, "The path {0} is too loong. The maximum path lenght is {1}", new object[] { targetPath, 260 }));
            }
            if (relativeToPath.Length > 260)
            {
                throw new ArgumentOutOfRangeException("relativeToPath", string.Format(CultureInfo.CurrentCulture, "The path {0} is too loong. The maximum path lenght is {1}", new object[] { relativeToPath, 260 }));
            }
            StringBuilder builder1 = new StringBuilder(520, 520);
            if (PathRelativePathTo(builder1, relativeToPath, FILE_ATTRIBUTE_DIRECTORY, targetPath, 0))
            {
                builder1.Replace(".\\", "");
                return builder1.ToString();
            }
            return targetPath;
        }

        public static string GetShortFileName(Store store, string longPath)
        {
            string text1 = Path.GetFileName(longPath);
            for (int num1 = 0; num1 < FileSystemInterop.invalidShortPathChars.Length; num1++)
            {
                text1 = text1.Replace(FileSystemInterop.invalidShortPathChars[num1].ToString(), string.Empty);
            }
            string text2 = Path.GetFileNameWithoutExtension(text1);
            if (text2.Length > 8)
            {
                object val;
                Dictionary<string, int> dictionary1;
                store.PropertyBag.TryGetValue("shortNameTable", out val);
                if (val == null)
                {
                    dictionary1 = new Dictionary<string, int>();
                    store.PropertyBag["shortNameTable"] = dictionary1;
                }
                else
                {
                    dictionary1 = (Dictionary<string, int>)val;
                }
                text2 = text2.Substring(0, 8);
                int num2 = 1;
                if (dictionary1.ContainsKey(text2))
                {
                    num2 = dictionary1[text2] + 1;
                }
                dictionary1[text2] = num2;
                string text3 = num2.ToString(CultureInfo.CurrentCulture);
                text2 = text2.Remove(text2.Length - text3.Length);
                text2 = text2 + text3;
            }
            string text4 = Path.GetExtension(text1);
            if (text4.Length > 4)
            {
                text4 = text4.Substring(0, 4);
            }
            return (text2 + text4);
        }

    }
}
